package com.easyadmin.modules.sys.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.easyadmin.common.constant.SystemConstant;
import com.easyadmin.common.context.UserContext;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.modules.sys.domain.SysRole;
import com.easyadmin.modules.sys.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.easyadmin.modules.sys.mapper.BOrderDispatchMapper;
import com.easyadmin.modules.sys.domain.BOrderDispatch;
import com.easyadmin.modules.sys.service.IBOrderDispatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 订单派单记录Service业务层处理
 *
 * @author mars
 * @date 2025-02-11
 */
@Service
@AllArgsConstructor
public class BOrderDispatchServiceImpl extends ServiceImpl<BOrderDispatchMapper, BOrderDispatch> implements IBOrderDispatchService {

    private final SysUserService sysUserService;


    /**
     * 查询订单派单记录
     *
     * @param bOrderDispatch 订单派单记录
     * @param pageDomain
     * @return 订单派单记录 分页集合
     */
    @Override
    public PageInfo<BOrderDispatch> selectBOrderDispatchPage(BOrderDispatch bOrderDispatch, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        // 获取当前用户ID
        String userId = UserContext.currentUser().getUserId();
        // 查询角色
        List<SysRole> userRoles = sysUserService.getUserRole(userId);
        boolean b = userRoles.stream().anyMatch(x -> x.getRoleCode().equals(SystemConstant.ADMIN_ROLE_CODE) && x.isChecked());
        // 非管理员设置用户ID
        if (!b) {
            bOrderDispatch.setUserId(userId);
        }
        List<BOrderDispatch> data = baseMapper.selectBOrderDispatchList(bOrderDispatch);
        return new PageInfo<>(data);
    }

}
